package jmx.standardmbean;

import javax.management.AttributeChangeNotification;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;

import sample.Util;

public class TomcatServerController extends NotificationBroadcasterSupport implements TomcatServerControllerMBean {
	private ServerInfo serverInfo;
	private long startTime;
	private long endTime;
	private boolean isServerRunning;
	private boolean isServerUpdationRequired;
	private int serverSleepTime = 5;

	public int getServerSleepTime() {
		return serverSleepTime;
	}

	public void setServerSleepTime(int serverSleepTime) {
		notifyAttributeChange("ServerSleepTime is changed ", this.serverSleepTime, serverSleepTime);
		this.serverSleepTime = serverSleepTime;
	}

	int notificationSeqNumber = 0;

	public TomcatServerController() {
		System.out.println("TomcatServerController Constructor Called ...");
		serverInfo = new ServerInfo("TOMCAT", "7", "APACHE");
	}

	public void startServer(String adminName) throws InterruptedException {
		startTime = System.currentTimeMillis();
		System.out.println("Started at :" + startTime + " by : " + adminName);
		if (isServerRunning == false) {
			isServerRunning = true;
			notificationSeqNumber += notificationSeqNumber;
			notify("Server started");
			Thread.sleep(1000 * 60 * serverSleepTime);
		} else
			System.out.println("Server is already running ");
	}

	@Override
	public void stopServer(String reason) {
		endTime = System.currentTimeMillis();
		System.out.println("Server Stopped at : " + endTime + " due to : " + reason);
		isServerRunning = false;
		notificationSeqNumber += notificationSeqNumber;
		notify("Server stopped due to : " + reason);
	}

	public void notify(String msg) {
		Notification notification = new Notification("com.example.notification" + "." + msg, this,
				notificationSeqNumber, System.currentTimeMillis(), msg);
		Object userData = serverInfo;
		notification.setUserData(userData);
		sendNotification(notification);
	}

	public void notifyAttributeChange(String msg, Object oldValue, Object newValue) {
		AttributeChangeNotification attributeChangeNotification = new AttributeChangeNotification(this, -1,
				System.currentTimeMillis(), msg, "isServerUpdationRequired", "com.example.versionupdation", oldValue,
				newValue);
		Object userData = serverInfo;
		attributeChangeNotification.setUserData(userData);
		sendNotification(attributeChangeNotification);
		Util.echo("Attribute Change result");
	}

	@Override
	public void setUpdateServerVersion() {
		this.isServerUpdationRequired = true;
		notify("Server Version Updation required ...");
	}

	@Override
	public long getDummyAttribute() {
		return 35;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public long getEndTime() {
		return endTime;
	}

	public void setEndTime(long endTime) {
		this.endTime = endTime;
	}

	public boolean isServerRunning() {
		return isServerRunning;
	}

	public ServerInfo getServerInfo() {
		return serverInfo;
	}

}
